package com.hkts.naturalgas.workovertime.controller;


import com.hkts.naturalgas.constant.HkOverTimeConstant;
import com.hkts.naturalgas.constant.PerformanceConstant;
import com.hkts.naturalgas.feign.FeignInterface;
import com.hkts.naturalgas.performance.performanceTo.HontUserTo;
import com.hkts.naturalgas.performance.performanceTo.SubmitPerformanceDTo;
import com.hkts.naturalgas.performance.performanceVo.PerformanceInfoNameAndUserNickVo;
import com.hkts.naturalgas.utils.IDUtils;
import com.hkts.naturalgas.workovertime.VO.HkOvertimeDetailVo;
import com.hkts.naturalgas.workovertime.VO.HkOvertimeInfoNameAndUserNickVo;
import com.hkts.naturalgas.workovertime.VO.HkassessmentOvertimeAndRecordVo;
import com.hkts.naturalgas.workovertime.service.IHkOvertimeInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import com.hkts.naturalgas.utils.CustomPageUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hkts.naturalgas.utils.Result;
import com.hkts.naturalgas.utils.ResultUtil;
import com.alibaba.fastjson.JSON;
import org.springframework.ui.Model;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hkts.naturalgas.workovertime.service.IHkOvertimeDetailService;
import com.hkts.naturalgas.workovertime.entity.HkOvertimeDetail;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * @author hml
 * @since 2023-08-31
 */
@Slf4j
@RestController
@RequestMapping("/performance/hkOvertimeDetail")
public class HkOvertimeDetailController {
    private final Logger logger = LoggerFactory.getLogger(HkOvertimeDetailController.class);

    @Autowired
    public IHkOvertimeDetailService iHkOvertimeDetailService;
    @Autowired
    public RedisTemplate redisTemplate;
    @Autowired
    private FeignInterface feignInterface;
    @Autowired
    private IHkOvertimeInfoService iHkOvertimeInfoService;

    /**
     * 分页查询数据
     *
     * @param page             分页信息
     * @param hkOvertimeDetail 查询条件
     * @return
     */
    @ResponseBody
    @GetMapping("/getHkOvertimeDetailPageList")
    public Result<Object> getHkOvertimeDetailPageList(Page<HkOvertimeDetail> page, HkOvertimeDetail hkOvertimeDetail) {
        return ResultUtil.data(CustomPageUtils.getCustomPage(iHkOvertimeDetailService.selectPage(page, hkOvertimeDetail)));
    }

    /**
     * 提交加班/调休信息
     *
     * @param hkOvertimeDetailVo 传递的实体
     * @return saveOrUpdate
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/hkOvertimeDetailSave")
    public Result<Object> hkOvertimeDetailSave(@RequestBody HkOvertimeDetailVo hkOvertimeDetailVo) throws Exception {
        log.info("请求时长：{}",hkOvertimeDetailVo.getTotalHours());
        List<String> stringList = iHkOvertimeDetailService.saveOverTimeDetail(hkOvertimeDetailVo);
        if (stringList.size() > 0){
            String join = String.join(",", stringList);
            return ResultUtil.success(join + "剩余调休时长不足！");
        }
        return ResultUtil.success("保存成功");
    }

    /*
     *
     * 分数修改并审核(v1.5)
     * */
    @ResponseBody
    @PostMapping("/approvalWithOvertime")
    public Result approvalWithOvertime(@RequestBody SubmitPerformanceDTo submitPerformanceDTo) throws Exception {
        Map<String, Object> map = iHkOvertimeDetailService.approvalWithOvertime(submitPerformanceDTo);
        return ResultUtil.data(map);
    }

    /*
     * 获取重复防止重复提交的key
     * */
    @ResponseBody
    @GetMapping("/getSubmissionKey")
    public Result getSubmissionKey(){
        HontUserTo hontUserTo = feignInterface.remoteUserInfo();
        String s = IDUtils.genImageName();
        redisTemplate.opsForValue().set(HkOverTimeConstant.SUBMISSION_KEY+ s,hontUserTo.getId(),48, TimeUnit.HOURS);
        return ResultUtil.data(s);
    }

    /*
     * 待办数量
     * */
    @GetMapping(value = "/get-performance-applying-tasks-num")
    public Result  getPerformanceApplyingTasksNum() {
        HontUserTo hontUserTo = feignInterface.remoteUserInfo();
        List<String> dealtWith = iHkOvertimeInfoService.getDealtWith(hontUserTo.getId());
        Map mm=new HashMap();
        mm.put("num",dealtWith.size());
        return ResultUtil.data(mm);
    }

    /*
     * 待办列表
     * */
    @ResponseBody
    @GetMapping("/getHkAssessmentInfoAndUserNickNameByProIds")
    public Result<List<HkOvertimeInfoNameAndUserNickVo>> getHkOvertimeInfoAndUserNickNameByProIds() {
        Result<List<HkOvertimeInfoNameAndUserNickVo>> hkOvertimeInfoAndUserNickNameByProIds = iHkOvertimeDetailService.getHkOvertimeInfoAndUserNickNameByProIds();
        return hkOvertimeInfoAndUserNickNameByProIds;
    }


    /**
     * processInstanceId
     *
     * @param processInstanceId 实体ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.GET, value = "/getHkOvertimeDetial")
    public Result<Object> getHkOvertimeDetial(String processInstanceId) {
        HkassessmentOvertimeAndRecordVo byPrcessId = iHkOvertimeDetailService.getHkOvertimeDetial(processInstanceId);
        return ResultUtil.data(byPrcessId);
    }

    /**
     * 根据id获取对象
     *
     * @param id 实体ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.GET, value = "/hkOvertimeDetailGetById")
    public Result<Object> hkOvertimeDetailGetById(String id) {
        HkOvertimeDetail byId = iHkOvertimeDetailService.getById(id);
        return ResultUtil.data(byId);
    }

    /**
     * 根据id删除对象
     *
     * @param id 实体ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/hkOvertimeDetailDelete")
    public Result<Object> hontUserDelete(String id) {
        iHkOvertimeDetailService.removeById(id);
        return ResultUtil.success("删除成功");
    }

    /**
     * 批量删除对象
     *
     * @param item 实体集合ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/hkOvertimeDetailBatchDelete")
    public Result<Object> deleteBatchIds(String item) {
        List<String> ids = (List<String>) JSON.parse(item);
        iHkOvertimeDetailService.removeByIds(ids);
        return ResultUtil.success("删除成功");
    }



}
